//
//  LGCancerPatientSymptomCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/22.
//

#import "LGCancerPatientSymptomCell.h"

@interface LGCancerPatientSymptomCell()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIButton *checkBtn;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;
/**   */
@property (nonatomic , assign)CGFloat maxWidth;

@end

@implementation LGCancerPatientSymptomCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.maxWidth = Screen_W-viewPix(56);
        [self createSubView];
    }
    return  self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = LGNSString(dataDic[@"symptom_name"]);
    self.timeLabel.text = [NSString stringWithFormat:@"上次记录时间 %@",dataDic[@"record_date"]];
    NSArray *itemArray = dataDic[@"treat_types"];
    for (UILabel *label in self.itemArray) {
        [label removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    CGFloat maxWidth = Screen_W-viewPix(56);
    CGFloat left = 0;
    CGFloat top = 0;
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSString *content = LGNSString(itemArray[i]);
        CGFloat contentW = [content textWidthWithFont:LGFont(12) maxHeight:MAXFLOAT]+viewPix(16);
        top = (left+contentW)>maxWidth?top+viewPix(24):top;
        left = (left+contentW)>maxWidth?0:left;
        UILabel *label = [UILabel lableWithText:content colorString:@"#5E8EF5" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        label.frame = CGRectMake(left, top, contentW, viewPix(16));
        label.backgroundColor = [UIColor colorWithString:@"#F0F3FD"];
        label.cornerRidus = viewPix(8);
        [self.itemView addSubview:label];
        [self.itemArray addObject:label];
        left = kMaxX(label.frame)+viewPix(8);
    }
    CGFloat itemH = (itemArray.count>0)?top+viewPix(16):0;
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(itemH));
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.type = LGPositionTypeSingle;
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.checkBtn];
    [self.baseView addSubview:self.itemView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.checkBtn.mas_left).offset(-viewPix(5));
    }];
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.titleLabel);
        make.width.equalTo(@(viewPix(80)));
        make.height.equalTo(@(viewPix(30)));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.checkBtn);
    }];
    
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.timeLabel);
        make.bottom.equalTo(self.baseView.mas_bottom).offset(-viewPix(12));
        make.height.equalTo(@(0));
    }];
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _timeLabel;
}

-(UIButton *)checkBtn{
    if(!_checkBtn){
        _checkBtn = [UIButton buttonWithTitle:@"查看全部" titleFont:LGFont(13) textColor:@"#909399" imageName:@"grayTip16"];
        _checkBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _checkBtn.bounds = CGRectMake(0, 0, viewPix(80), viewPix(30));
        [_checkBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
        _checkBtn.userInteractionEnabled = NO;
    }
    return _checkBtn;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc]init];
    }
    return _itemView;
}


@end
